<template>
	<view class="page-body">
		<!-- 页面内容开始 -->
		<!-- 表格搜索组件开始 -->
		<vk-data-table-query
			v-model="queryForm1.formData"
			:columns="queryForm1.columns"
			size="mini"
			@search="search"
		></vk-data-table-query>
		<!-- 表格搜索组件结束 -->

		<view>
			<el-row>
				<el-dropdown v-if="table1.multipleSelection" :split-button="false"	trigger="click" @command="batchBtn">
					<el-button type="danger" size="small" style="margin-left: 20rpx;"
						:disabled="table1.multipleSelection.length === 0"
					>
						批量操作<i class="el-icon-arrow-down el-icon--right"></i>
					</el-button>
				  <el-dropdown-menu slot="dropdown">
						<el-dropdown-item :command="1">批量审核通过</el-dropdown-item>
						<el-dropdown-item :command="2">批量审核拒绝</el-dropdown-item>
				  </el-dropdown-menu>
				</el-dropdown>
			</el-row>
		</view>

		<!-- 表格组件开始 -->
		<vk-data-table
			ref="table1"
			:action="table1.action"
			:columns="table1.columns"
			:query-form-param="queryForm1"
			:right-btns="['detail_auto','delete']"
			:custom-right-btns="[
				{ title:'审核', type:'primary', icon:'el-icon-edit' }
			]"
			size="mini"
			:row-no="true"
			:pagination="true"
			:selection="true"
			@delete="deleteBtn"
			@current-change="currentChange"
			@selection-change="selectionChange"
			@custom-right-btns="customRightBtns"
		></vk-data-table>
		<!-- 表格组件结束 -->

		<!-- 添加或编辑的弹窗开始 -->
		<vk-data-dialog
			v-model="formData1.show"
			:title="formData1.title"
			width="500px"
			mode="form"
		>
			<vk-data-form
				v-model="form1"
				:rules="formData1.rules"
				:action="formData1.action"
				:form-type="formData1.formType"
				:columns='formData1.columns'
				label-width="90px"
				@success="examineSuccess"
			>
			<template
				slot="content"
			>
				<view>
					<view style="display: flex;align-items: center;"  v-if="form1.user_info">
						<el-image :src="form1.user_info.avatar" fit="cover" class="image-avatar"></el-image>
						<view style="margin-left: 8px;">
							{{form1.user_info.nickname}} :
						</view>
					</view>
					<view style="line-height: 1.5;">
						{{form1.content}}
					</view>
				</view>
			</template>

			</vk-data-form>
		</vk-data-dialog>
		<!-- 添加或编辑的弹窗结束 -->

		<!-- 页面内容结束 -->
	</view>
</template>

<script>
	var that;													// 当前页面对象
	var vk = uni.vk;									// vk实例
	var originalForms = {};						// 表单初始化数据

	export default {
		data() {
			// 页面数据变量
			return {
				// 页面是否请求中或加载中
				loading:false,
				// init请求返回的数据
				data:{

				},
				// 表格相关开始 -----------------------------------------------------------
				table1:{
					// 表格数据请求地址
					action:"admin/business/sys/goodsComment.getList",
					// 表格字段显示规则
					columns:[
						{ key:"goods_name", title:"商品名称", type:"text", minWidth:180 },
						{ key:"sku_name", title:"sku名称", type:"text", minWidth:100 },
						{ key:"status", title:"评价状态", type:"tag", width:90, size:"small",
							data:[
								{ value:-1, label:"审核拒绝", tagType:"danger" },
								{ value:0, label:"待审核", tagType:"info" },
								{ value:1, label:"审核通过", tagType:"success" }
							]
						},
						{ key:"user_info", title:"评价者", type:"userInfo", minWidth:90 },
						{ key:"content", title:"评价内容", type:"text", minWidth:290 },
						{ key:"images", title:"图片", type:"image", width:160 },
						{ key:"publish_ip", title:"IP地址", type:"text", width:150 },
						{ key:"stars", title:"评分", type:"number", width:120 },
						{ key:"_add_time", title:"添加时间", type:"time", width:150, sortable:"custom" },
					],
					// 多选框选中的值
					multipleSelection:[],
					// 当前高亮的记录
					selectItem:"",
				},
				// 表格相关结束 -----------------------------------------------------------
				// 表单相关开始 -----------------------------------------------------------
				// 查询表单请求数据
				queryForm1:{
					// 查询表单数据源，可在此设置默认值
					formData:{
						goods_id:''
					},
					// 查询表单的字段规则 fieldName:指定数据库字段名,不填默认等于key
					columns:[
						{ key:"user_id", title:"用户ID", type:"remote-select", placeholder:"请输入用户ID",width:140,
							action:"admin/select/kh/user"
						},
						{ key:"status", title:"评价状态", type:"select",width:140,
							data:[
								{ value:-1, label:"审核拒绝" },
								{ value:0, label:"待审核" },
								{ value:1, label:"审核通过" },
							]
						},
						{ key:"goods_id", title:"商品id", type:"text", mode:"=", width:140 },
						{ key:"user_nickname", title:"评价者昵称", type:"text", mode:"%%", width:140, fieldName:"user_info.nickname" },
						{ key:"content", title:"评价内容", type:"text", mode:"%%", width:140 },
						{ key:"_add_time", title:"添加时间", type:"datetimerange", width:340 },
					]
				},
				// 表单数据源，可在此设置默认值
				form1:{

				},
				formData1:{
					title:"审核",
					// 表单请求地址
					action:"admin/business/sys/goodsComment.update",
					// 表单字段显示规则
					columns:[
						{ key:"goods_name", title:"商品名称", type:"text-view" },
						{ key:"sku_name", title:"SKU名称", type:"text-view" },
						{ key:"content", title:"评价内容", type:"text-view" },
						{ key:"images", title:"图片", type:"image", disabled: true },
						{ key:"status", title:"状态", type:"radio",
							data:[
								{ value:-1, label:"审核拒绝" },
								{ value:0, label:"待审核" },
								{ value:1, label:"审核通过" },
							]
						},
					],
					// 表单验证规则
					rules:{
						status:[
							{ required:true, message:"状态不能为空", trigger:"blur" },
							{ type:"number", message:"状态必须是数字", trigger:"blur" },
						],
					},
					// add 代表添加 update 代表修改
					formType:"update",
					// 是否显示表单的弹窗
					show:false
				},
				// 表单相关结束 -----------------------------------------------------------
			};
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady() {},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options) {
				originalForms["form1"] = vk.pubfn.copyObject(that.form1);
				if(options._id) that.queryForm1.formData.goods_id = options._id;
			},
			// 页面跳转
			pageTo(path) {
				vk.navigateTo(path);
			},
			// 页面返回
			pageBack(path){
				const pages = getCurrentPages();
				if (pages.length>1 && pages[0].route !== that.$page.route) {
					vk.navigateBack();
				} else if(path) {
					vk.navigateTo(path);
				}
			},
			// 表单重置
			resetForm(){
				vk.pubfn.resetForm(originalForms, that);
			},
			// 搜索
			search(){
				that.$refs.table1.query();
			},
			// 刷新
			refresh(){
				that.$refs.table1.refresh();
			},
			// 获取当前选中的行的数据
			getCurrentRow(key){
				return that.$refs.table1.getCurrentRow(key);
			},
			// 监听 - 行的选中高亮事件
			currentChange(val){
				that.table1.selectItem = val;
			},
			// 当选择项发生变化时会触发该事件
			selectionChange(list) {
				that.table1.multipleSelection = list;
			},
			// 删除按钮
			deleteBtn({ item, deleteFn }){
				deleteFn({
					action:"admin/business/sys/goodsComment.delete",
					data:{
						_id: item._id
					},
				});
			},
			customRightBtns(item,btn){
				if(btn.title === "审核"){
					that.examine(item);
				}
			},
			// 显示修改页面
			examine(item){
				that.formData1.show = true;
				that.form1 = item;
			},
			examineSuccess({ data }){
				let item = that.getCurrentRow(true);
				that.formData1.show = false;
				item.status = data.status;
				//that.refresh();
			},
			batchBtn(index){
				switch(index){
					case 1: that.batchAudit(1); break;
					case 2: that.batchAudit(-1); break;
					default : break;
				}
			},
			batchAudit(status){
				// status为修改的状态
				let ids = [];
				that.table1.multipleSelection.map((item,index)=>{
					ids.push(item._id)
				});
				let dataJson = {
					ids, status
				};
				vk.callFunction({
					url: 'admin/business/sys/goodsComment.update',
					title:'请求中...',
					data:dataJson,
					success:function(data) {
						that.$notify({
							message:'批量操作成功!',
							type:'success'
						});
						that.refresh();
					},
				});
			},
		},
		// 监听属性
		watch: {
			
		},
		// 计算属性
		computed: {

		}
	};
</script>
<style lang="scss" scoped>
	.page-body {

	}
</style>
